﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Happy.Archetype;
using Happy.Query;
using Happy.Application.Query;
using Happy.Cms.Query.Articles;
using Happy.Cms.Application.Categories;

namespace Happy.Cms.Application.Articles
{
    [QueryService]
    public sealed class ArticleQueryService : QueryService<ArticleView>
    {
        protected override Happy.Query.Filters.FilterDefinition FilterDefinition
        {
            get
            {
                return null;
            }
        }

        public ArticleView FindByTitle(string title)
        {
            var query = QueryArgument.Create(new { Title = title });

            return this.QuerySource.SingleOrDefault(query);
        }

        public IEnumerable<ArticleView> FindByCategoryName(string categoryName)
        {
            var category = this.Service<CategoryQueryService>().FindByName(categoryName);

            return this.FindByCategoryId(category.Id);
        }

        public IEnumerable<ArticleView> FindByCategoryName(string categoryName, int top)
        {
            var category = this.Service<CategoryQueryService>().FindByName(categoryName);

            return this.FindByCategoryId(category.Id, top);
        }

        public PageResult<ArticleView> FindByCategoryName(string categoryName, long page, long itemsPerPage)
        {
            var category = this.Service<CategoryQueryService>().FindByName(categoryName);

            return this.FindByCategoryId(category.Id, page, itemsPerPage);
        }

        public IEnumerable<ArticleView> FindByCategoryId(Guid categoryId)
        {
            var query = QueryArgument.Create(new { CategoryId = categoryId });

            return this.QuerySource.Fetch(query);
        }

        public IEnumerable<ArticleView> FindByCategoryId(Guid categoryId, int top)
        {
            var query = QueryArgument.Create(new { CategoryId = categoryId });

            return this.QuerySource.SkipTake(query, 0, top);
        }

        public PageResult<ArticleView> FindByCategoryId(Guid categoryId, long page, long itemsPerPage)
        {
            var query = QueryArgument.Create(new { CategoryId = categoryId });

            return this.QuerySource.Page(query, page, itemsPerPage);
        }
    }
}
